package br.com.moove.bananasfit.view;

import org.json.JSONObject;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import br.com.moove.bananasfit.R;
import br.com.moove.bananasfit.control.VolleySingleton;
import br.com.moove.bananasfit.model.PessoaFisica;
import br.com.moove.bananasfit.model.PessoaJuridica;
import br.com.moove.bananasfit.model.Servico;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

public class DetalhesPessoaJuridicaFragment extends Fragment implements
		OnRatingBarChangeListener, Response.Listener<JSONObject>,
		Response.ErrorListener {

	PessoaJuridica pessoaJuridica;
	PessoaFisica usuario;

	TextView txtNomePJuridica;
	NetworkImageView imgPJuridica;
	TextView txtDetalhesPJuridica;
	TextView txtServicosPJuridica;
	TextView txtFonesPJuridica;
	RatingBar ratingBarPJuridica;
	TextView txtEnderecoPJuridica;
	TextView txtTelefonesPessoaJuridica;

	NetworkImageView imageView1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View layout = inflater.inflate(R.layout.detalhes_pjuridica_fragment,
				null);
		pessoaJuridica = (PessoaJuridica) getArguments().getSerializable(
				"pessoaFisica");
		usuario = (PessoaFisica) getArguments().getSerializable("Usuario");
		
		txtNomePJuridica = (TextView) layout
				.findViewById(R.id.txtNomePJuridica);
		txtNomePJuridica.setText(pessoaJuridica.getNome());
		imgPJuridica = (NetworkImageView) layout.findViewById(R.id.imageView1);
		imgPJuridica.setImageUrl(pessoaJuridica.getImgUrl(), VolleySingleton
				.getInstance(getActivity()).getImageLoader());
		txtDetalhesPJuridica = (TextView) layout
				.findViewById(R.id.txtDetalhesPJuridica);
		txtDetalhesPJuridica.setText(pessoaJuridica.getDescricao());
		txtTelefonesPessoaJuridica = (TextView) layout
				.findViewById(R.id.txtTelefonesPessoaJuridica);
		txtTelefonesPessoaJuridica.setText(pessoaJuridica.getTelefone());
		txtServicosPJuridica = (TextView) layout
				.findViewById(R.id.txtServicosPessoaJuridica);
		txtServicosPJuridica.setText(pessoaJuridica.getServicosPessoaJuridica());
		
		/*txtFonesPJuridica = (TextView) layout.findViewById(R.id.txtHorariosPJuridica);
		txtFonesPJuridica.setText(pessoaJuridica.getCelular() + " " + pessoaJuridica.getTelefone());*/
		ratingBarPJuridica = (RatingBar) layout
				.findViewById(R.id.ratingBarPJuridica);
		ratingBarPJuridica.setStepSize(1);
		ratingBarPJuridica.setOnRatingBarChangeListener(this);
		ratingBarPJuridica.setRating(pessoaJuridica.getAvaliacao());
		txtEnderecoPJuridica = (TextView) layout
				.findViewById(R.id.txtEnderecoPJuridica);
		
		String endereco = pessoaJuridica.getEndereco().getRua() + ", " + pessoaJuridica.getEndereco().getNumero() + ", ";
		if (pessoaJuridica.getEndereco().getComplemento().equals(null) ){
			endereco += pessoaJuridica.getEndereco().getComplemento() + ", ";
		}
		endereco +=  pessoaJuridica.getEndereco().getBairro() + ", " + pessoaJuridica.getEndereco().getCidade() + ", " + pessoaJuridica.getEndereco().getEstado() + ", " + pessoaJuridica.getEndereco().getCep();
		txtEnderecoPJuridica.setText(endereco);
		return layout;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}

	public static DetalhesPessoaJuridicaFragment getInstance(
			PessoaJuridica pessoaJ, PessoaFisica pessoaF) {
		Bundle args = new Bundle();
		args.putSerializable("pessoaFisica", pessoaJ);
		args.putSerializable("Usuario", pessoaF);
		DetalhesPessoaJuridicaFragment f = new DetalhesPessoaJuridicaFragment();
		f.setArguments(args);
		return f;
	}

	@Override
	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		// TODO Auto-generated method stub
		try {
			JSONObject json = new JSONObject();
			json.put("PessoaJuridica", pessoaJuridica.getId());
			json.put("Pontuacao", rating);
			json.put("PessoaFisica", usuario.getId());
			RequestQueue queue = Volley.newRequestQueue(getActivity());
			JsonObjectRequest jsObjRequest = new JsonObjectRequest(
					Request.Method.POST,
					"http://bananasfit.com.br/api/avaliacaoapi/avaliar", json,
					this, this);
			int socketTimeout = 30000;// 30 seconds - change to what you
			// want
			RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
			DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
			DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
			jsObjRequest.setRetryPolicy(policy);	
			queue.add(jsObjRequest);
			Log.d("JSON", json.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void onErrorResponse(VolleyError error) {
		// TODO Auto-generated method stub
		Log.d("ErrorResponse", error.toString());
	}

	@Override
	public void onResponse(JSONObject response) {
		// TODO Auto-generated method stub
		Log.d("ErrorResponse", "Pegou");
	}
}
